﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;

namespace iLangl.LanguageWire.DAL.LanguageWireDAL.MockupStorage
{
	public class MockupInvoiceDAL : MockupBaseDAL, IInvoiceDAL
	{
		#region IInvoiceDAL Members

		public void CreateInvoice(BillingInfo billingInfo)
		{
			
				// If object already registered at LW side we skip registration
				if (!String.IsNullOrEmpty(billingInfo.TPIdentity))
					return;
			

			//Save Billing Info
			dtoMockupStorage.BillingInfoRow billingrow = Storage.BillingInfo.FindById(billingInfo.Id);

			bool isNew = (billingrow == null);
			if (isNew)
			{
				billingrow = Storage.BillingInfo.NewBillingInfoRow();
			}

		


			billingrow = Storage.BillingInfo.NewBillingInfoRow();

			BillingInfo bi = billingInfo;

			billingrow.Id = bi.Id;
			billingrow.CompanyId =  new Guid(billingInfo.Company.Id);

			if (!string.IsNullOrEmpty(bi.Name))
				billingrow.Name = bi.Name;

			if (!string.IsNullOrEmpty(bi.BillingFirstName))
				billingrow.BillingFirstName = bi.BillingFirstName;

			if (!string.IsNullOrEmpty(bi.BillingLastName))
				billingrow.BillingLastName = bi.BillingLastName;

			if (!string.IsNullOrEmpty(bi.BillingEmail))
				billingrow.BillingEmail = bi.BillingEmail;

			if (!string.IsNullOrEmpty(bi.BillingCountry))
				billingrow.BillingCountry = bi.BillingCountry;

			if (!string.IsNullOrEmpty(bi.BillingStreetAddress))
				billingrow.BillingStreetAddress = bi.BillingStreetAddress;

			if (!string.IsNullOrEmpty(bi.BillingStreetAddress2))
				billingrow.BillingStreetAddress2 = bi.BillingStreetAddress2;

			if (!string.IsNullOrEmpty(bi.BillingPostalCode))
				billingrow.BillingPostalCode = bi.BillingPostalCode;

			if (!string.IsNullOrEmpty(bi.BillingCity))
				billingrow.BillingCity = bi.BillingCity;

			if (!string.IsNullOrEmpty(bi.BillingCurrency))
				billingrow.BillingCurrency = bi.BillingCurrency;

			if (isNew)
			{
				Storage.BillingInfo.AddBillingInfoRow(billingrow);
			}

			AcceptChanges();

			iLangl.LanguageWire.DAL.iLanglDAL.DALFacade.Instance.CompanyDAL.UpdateBillingInfoReference(bi.Id, billingrow.TPIdentity, null);
		}

		#endregion
	}
}